﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Server.Engines.Dungeons
{
	public static class DungeonManager
	{
		private static InternalTimer m_timer;

		private static List<DungeonInstance> m_instance;

		static DungeonManager()
		{
			m_instance = new List<DungeonInstance>();

			m_timer = new InternalTimer();


			m_timer.Start();
		}

		public static void RegisterDungeon(DungeonDescription description)
		{
			m_instance.Add(new DungeonInstance(description));
		}

		public static void UnRegisterDungeon(DungeonInstance instance)
		{
			m_instance.Remove(instance);
		}

		private class InternalTimer : Timer
		{
			public InternalTimer()
				: base(TimeSpan.Zero, TimeSpan.FromMinutes(5.0))
			{

			}

			protected override void OnTick()
			{
				List<DungeonInstance> activeInstance = new List<DungeonInstance>(DungeonManager.m_instance);

				DateTime closeInstanceTime = DateTime.Now.AddMinutes(-30.0);

				foreach (var instance in activeInstance)
				{
					if (instance.GetPlayerCount() == 0
						&& instance.GetLastActivityTime() < closeInstanceTime)
					{
						DungeonManager.UnRegisterDungeon(instance);
					}
				}
			}
		}
	}
}
